<template>
  <el-dialog :title="title" :close-on-click-modal="false" width="60vw" :visible.sync="visible"
             :append-to-body="true" @close="clearDataList()">
    <el-table v-loading="loading" :data="dataForm" border style="width: 100%;">
      <el-table-column type="index" align="center" width="60px" label="序号"></el-table-column>
      <el-table-column prop="jjxFl" align="center" label="奖金项分类"></el-table-column>
      <el-table-column prop="jjxName" align="center" label="奖金项名称"></el-table-column>
      <el-table-column prop="jjcZe" align="center" label="奖金项总额(元)">
        <template slot-scope="scope">
          {{ $formatNum(scope.row.jjcZe) }}
        </template>
      </el-table-column>
      <el-table-column prop="jjcYe" align="center" label="剩余可用余额(元)">
        <template slot-scope="scope">
          {{ $formatNum(scope.row.jjcYe) }}
        </template>
      </el-table-column>
      <el-table-column prop="jjcZt" label="控制操作" align="center" width="200">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.jjcZt"
            :active-value="1"
            :inactive-value="2"
            active-text="开启"
            inactive-text="关闭"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="stateChange(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
    </el-table>
    <div class="avue-crud__pagination">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
                     :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background
                     layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" plain @click="clearDataList">关闭</el-button>
    </span>
  </el-dialog>
</template>
<script>
import {getControllPage, updateJiangJinState} from '@/api/jiangjin/jiangjinjjcxxb'

export default {
  data() {
    return {
      deptId: '',
      deptName: '',
      title: '',
      visible: false,
      dataForm: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      loading: false
    }
  },
  created() {

  },
  methods: {
    init(deptId,deptName) {
      this.visible = true
      this.deptId = deptId
      this.deptName = deptName
      this.title = '奖金池控制--' + deptName
      this.getDataForm()
    },

    // 查询指定机构奖金池信息列表
    getDataForm() {
      this.loading = true
      getControllPage({
        current: this.pageIndex,
        size: this.pageSize,
        deptId: this.deptId
      }).then(response => {
        this.dataForm = response.data.data.records
        this.totalPage = response.data.data.total
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    clearDataList() {
      this.visible = false
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataForm()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataForm()
    },

    // 更改奖金项状态
    stateChange(row) {
      updateJiangJinState({
        jjcId: row.jjcId,
        zt: row.jjcZt
      }).then(respnose => {
        this.$message.closeAll()
        this.$message.success("操作成功")
      }).catch(() => {
        // 恢复开关原状态
        row.jjcZt === 1 ? row.jjcZt = 2 :  row.jjcZt = 1
      })
    }

  }
}

</script>

<style scoped>

</style>
